import main_menu from '../../../main_menu'
import { Swiper, Slide } from 'vue-swiper-component';

var that
var interval_obj
export default {
    data () {
        return this.datainit({
            slidelist: [],  // 上拉更多的数据存放数组
            cate: [],
            goods: [],
            hot_goods: [],
            group_number:"--",
            user_number:"--",
            search_name:"",
            user:{
                group_code:null
            }
        })
    },
    components: {main_menu,Swiper,Slide},

    mounted: function () {
        this.check()
        that = this
        that.$post("api/index/slide", null, function (list) {
            that.slidelist = list
            that.$nextTick(function () {

            })
        })
        that.$post("api/index/lists", null, function (data) {
            that.cate = data.cate
            let goods_list=data.goods
            let current_time=Date.parse(new Date())/1000;
            for(let i=0;i<goods_list.length;i++){
                if(goods_list[i].golist.length>0){
                    for(let j=0;j<goods_list[i].golist.length;j++){
                        goods_list[i].golist[j].endtime_str=that.setendtime_core(goods_list[i].golist[j]['end_timestamp'],current_time)
                    }
                }
            }
            let hot_goods_list=data.hot_goods
            for(let i=0;i<hot_goods_list.length;i++){
                if(hot_goods_list[i].golist.length>0){
                    for(let j=0;j<hot_goods_list[i].golist.length;j++){
                        hot_goods_list[i].golist[j].endtime_str=that.setendtime_core(hot_goods_list[i].golist[j]['end_timestamp'],current_time)
                    }
                }
            }
            that.hot_goods = hot_goods_list
            that.goods = goods_list
        })
        that.$post("api/index/statistics",null,function (data) {
            console.log(data)
            that.group_number=data.group
            that.user_number=data.user
        })
        interval_obj=setInterval(function () {
            that.setendtime();
        },1000)
    },
    beforeDestroy:function(){
        clearInterval(interval_obj)
    },
    methods: {
        user_init: function (_user) {
            that.user=_user
        },
        tosearch:function () {
            if(that.search_name==""){
                that.$toast("请输入搜索内容");
                return
            }
            that.$router.push({path: "/pages/goods/list", query: {search: that.search_name}})
        },
        setendtime:function () {
            let current_time=Date.parse(new Date())/1000;
            //console.log(current_time)
            let goods_list=that.goods
            for(let i=0;i<goods_list.length;i++){
                if(goods_list[i].golist.length>0){
                    for(let j=0;j<goods_list[i].golist.length;j++){
                        goods_list[i].golist[j].endtime_str=that.setendtime_core(goods_list[i].golist[j]['end_timestamp'],current_time)
                    }
                }
            }
            let hot_goods_list=that.hot_goods
            for(let i=0;i<hot_goods_list.length;i++){
                if(hot_goods_list[i].golist.length>0){
                    for(let j=0;j<hot_goods_list[i].golist.length;j++){
                        hot_goods_list[i].golist[j].endtime_str=that.setendtime_core(hot_goods_list[i].golist[j]['end_timestamp'],current_time)
                    }
                }
            }
        },
        setendtime_core:function (end_timestamp,current_time) {
            let time=end_timestamp-current_time;
            if(time<0){
                return "已结束"
            }
            let day = Math.floor(time / (60 * 60 * 24));
            let hour = Math.floor(time / (60 * 60)) - (day * 24);
            let minute = Math.floor(time / 60) - (day * 24 * 60) - (hour * 60);
            let second = Math.floor(time) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            if (day <= 9) day = '0' + day;
            if (hour <= 9) hour = '0' + hour;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            return "剩:<span>"+day+"</span>天<span>"+hour+"</span>:<span>"+minute+"</span>:<span>"+second+"</span>"
        }
    }
}